function [vis_snips,run_snips,run_snips_dark,fdbk_snips]=meta_analysis_find_onset_snips(proj_meta,cur_site,vis_fl,order,exp_inds,shad_all)

l_win=10;
r_win=5;
data_lwin=5;
data_rwin=35;
vis_snips={};
run_snips={};
% ffm_snips={};
fdbk_snips={};
run_snips_dark={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    cnt=1;
    runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
    runM(min(exp_inds{tnd}(find(order{tnd}==3),1)):max(exp_inds{tnd}(find(order{tnd}==3),2)))=0;
    vis_ons=find(diff(shad_all{tnd}==2)==1);
%     vis_ons=find(diff(vis_fl{tnd})==1);
    vis_ons(find(diff(vis_ons)<data_lwin+data_rwin)+1)=[];
    for gnd=1:length(vis_ons)
        if ~(vis_ons(gnd)-data_lwin<1)&&~(vis_ons(gnd)+data_rwin>length(shad_all{tnd}))
            if sum(shad_all{tnd}(vis_ons(gnd)-l_win:vis_ons(gnd)))==0 & sum(shad_all{tnd}(vis_ons(gnd)+1:vis_ons(gnd)+r_win)==2)==r_win
%             if sum(runM(vis_ons(gnd)-l_win:vis_ons(gnd)+r_win))==0 & sum(vis_fl{tnd}(vis_ons(gnd)-l_win:vis_ons(gnd)))==0 & sum(vis_fl{tnd}(vis_ons(gnd)+1:vis_ons(gnd)+r_win))==r_win
                vis_snips{tnd}(cnt,1)=vis_ons(gnd)-data_lwin;
                vis_snips{tnd}(cnt,2)=vis_ons(gnd)+data_rwin;
                cnt=cnt+1;
            end
        end
    end
    cnt=1;
    run_ons=find(diff(runM)==1);
    run_ons(find(diff(run_ons)<data_lwin+data_rwin)+1)=[];
    for gnd=1:length(run_ons)
        if ~(run_ons(gnd)-data_lwin<1)&&~(run_ons(gnd)+data_rwin>length(shad_all{tnd}))
            if sum(vis_fl{tnd}(run_ons(gnd)-l_win:run_ons(gnd)+r_win))==0 & sum(runM(run_ons(gnd)-l_win:run_ons(gnd)))==0 & sum(runM(run_ons(gnd)+1:run_ons(gnd)+r_win))==r_win
                run_snips{tnd}(cnt,1)=run_ons(gnd)-data_lwin;
                run_snips{tnd}(cnt,2)=run_ons(gnd)+data_rwin;
                run_ons_mat{tnd}(cnt)=run_ons(gnd);
                cnt=cnt+1;
            end
        end
    end
%     cnt=1;
%     cur_ffm=shad_all{tnd}==3;
%     to_throw=exp_inds{tnd}(find(order{tnd}==1),:);
%     for hnd=1:size(to_throw,1)
%         cur_ffm(to_throw(hnd,1):to_throw(hnd,2))=0;
%     end
%     ffm_ons=find(diff(cur_ffm)==1);
%     ffm_ons(find(diff(ffm_ons)<21)+1)=[];
%     %     ffm_ons(ismember(ffm_ons,run_ons))=[];
%     for gnd=1:length(ffm_ons)
%         if ~(ffm_ons(gnd)-l_win<0)&&~(ffm_ons(gnd)+r_win+10>length(runM))
%             if sum(cur_ffm(ffm_ons(gnd)-l_win:ffm_ons(gnd)-1))==0
%                 if sum(run_ons_mat{tnd}==ffm_ons(gnd))==0
%                     ffm_snips{tnd}(cnt,1)=ffm_ons(gnd)-l_win;
%                     ffm_snips{tnd}(cnt,2)=ffm_ons(gnd)+r_win;
%                     cnt=cnt+1;
%                 end
%             end
%         end
%     end
    cnt=1;
    cur_fdbk=shad_all{tnd}==1;
%     to_throw=exp_inds{tnd}(find(order{tnd}==1),:);
%     for hnd=1:size(to_throw,1)
%         cur_fdbk(to_throw(hnd,1):to_throw(hnd,2))=0;
%     end
    fdbk_ons=find(diff(cur_fdbk)==1);
    fdbk_ons(find(diff(fdbk_ons)<data_lwin+data_rwin)+1)=[];
    %     ffm_ons(ismember(ffm_ons,run_ons))=[];
    for gnd=1:length(fdbk_ons)
        if ~(fdbk_ons(gnd)-data_lwin<1)&&~(fdbk_ons(gnd)+data_rwin+10>length(shad_all{tnd}))
            if sum(shad_all{tnd}(fdbk_ons(gnd)-l_win:fdbk_ons(gnd)))==0 & sum(shad_all{tnd}(fdbk_ons(gnd)+1:fdbk_ons(gnd)+r_win)==1)==r_win
%             if sum(runM(fdbk_ons(gnd)-l_win:fdbk_ons(gnd)))==0 & sum(runM(fdbk_ons(gnd)+1:fdbk_ons(gnd)+r_win))==r_win & ...
%                     sum(vis_fl{tnd}(fdbk_ons(gnd)-l_win:fdbk_ons(gnd)))==0 & sum(vis_fl{tnd}(fdbk_ons(gnd)+1:fdbk_ons(gnd)+r_win))==r_win
                fdbk_snips{tnd}(cnt,1)=fdbk_ons(gnd)-data_lwin;
                fdbk_snips{tnd}(cnt,2)=fdbk_ons(gnd)+data_rwin;
                cnt=cnt+1;
            end
        end
    end
end

for tnd=1:size(proj_meta(cur_site).rd,2)
    runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
    runM(min(exp_inds{tnd}(find(order{tnd}~=3),1)):max(exp_inds{tnd}(find(order{tnd}~=3),2)))=0;
    cnt=1;
    run_ons=find(diff(runM)==1);
    run_ons(find(diff(run_ons)<data_lwin+data_rwin)+1)=[];
    for gnd=1:length(run_ons)
        if ~(run_ons(gnd)-data_lwin<0)&&~(run_ons(gnd)+data_rwin>length(runM))
            if sum(runM(run_ons(gnd)-l_win:run_ons(gnd)))==0 & sum(runM(run_ons(gnd)+1:run_ons(gnd)+r_win))==r_win
                run_snips_dark{tnd}(cnt,1)=run_ons(gnd)-data_lwin;
                run_snips_dark{tnd}(cnt,2)=run_ons(gnd)+data_rwin;
                run_ons_mat{tnd}(cnt)=run_ons(gnd);
                cnt=cnt+1;
            end
        end
    end
end
meta_check_onsets(proj_meta,cur_site,shad_all,vis_fl,vis_snips)
meta_check_onsets(proj_meta,cur_site,shad_all,vis_fl,run_snips)
meta_check_onsets(proj_meta,cur_site,shad_all,vis_fl,fdbk_snips)